package com.cyj.spring.springframeword.beans.factory.support;

import com.cyj.spring.springframeword.beans.BeansException;
import com.cyj.spring.springframeword.beans.factory.config.BeanDefinition;

/**
 * @Description: 自动装配bean工厂
 * @BelongsProject: small-spring
 * @BelongsPackage: com.cyj.spring.springframeword.beans.factory.support
 * @Author: ChenYongJia
 * @CreateTime: 2021-07-26 14:35
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {

    /**
     * 创建 bean 容器
     *
     * @param beanName bean名称
     * @param beanDefinition bean定义
     * @return java.lang.Object
     * @date 2021/7/27 15:30
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException {
        Object bean = null;
        try {
            bean = beanDefinition.getBeanClass().newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new BeansException("Instantiation of bean failed", e);
        }

        addSingleton(beanName, bean);
        return bean;
    }

}
